package com.chl.legou.seckill.service.impl;

import com.chl.legou.common.utils.IdWorker;
import com.chl.legou.common.utils.SystemConstants;
import com.chl.legou.core.service.impl.CrudServiceImpl;
import com.chl.legou.seckill.dao.SeckillGoodsDao;
import com.chl.legou.seckill.po.SeckillGoods;
import com.chl.legou.seckill.po.SeckillOrder;
import com.chl.legou.seckill.po.SeckillStatus;
import com.chl.legou.seckill.service.ISeckillOrderService;
import com.chl.legou.seckill.task.MultiThreadingCreateOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author chlong
 * @date 2022/10/21
 */
@Service
public class SeckillOrderServiceImpl extends CrudServiceImpl<SeckillOrder> implements ISeckillOrderService {

    @Autowired
    private MultiThreadingCreateOrder multiThreadingCreateOrder;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SeckillGoodsDao seckillGoodsDao;

    @Autowired
    private IdWorker idWorker;

    /***
     * 添加秒杀订单
     * @param id :商品ID
     * @param time :商品秒杀开始时间
     * @param username :⽤户登录名
     * @return
     */
    @Override
    public Boolean add(Long id, String time, String username) {
        /*
         在redis中存储⽤户是否抢单,结构：
         namespace UserQueueCount
         - username 次数
        */
        Long increment = redisTemplate.boundHashOps(SystemConstants.SEC_KILL_QUEUE_REPEAT_KEY).increment(username, 1);
        //判断 是否⼤于1 如果是,返回 ,否则 就放⾏ 重复了.
        if (increment > 1){
            throw new RuntimeException("20006");
        }

        //排队信息封装
        SeckillStatus seckillStatus = new SeckillStatus(username, new Date(), 1, id, time);
        //将秒杀抢单信息存⼊到Redis中,这⾥采⽤List⽅式存储,List本身是⼀个队列
        redisTemplate.boundListOps(SystemConstants.SEC_KILL_USER_QUEUE_KEY).leftPush(seckillStatus);

        //多线程操作
        multiThreadingCreateOrder.createOrder();

//        //获取商品数据
//        SeckillGoods goods = (SeckillGoods) redisTemplate.boundHashOps(SystemConstants.SEC_KILL_GOODS_PREFIX + time).get(id);
//        //无数据，直接抛出异常
//        if (goods == null || goods.getStockCount() <= 0){
//            throw new RuntimeException("已售罄！");
//        }
//        //有库存，创建秒杀商品
//        SeckillOrder seckillOrder = new SeckillOrder();
//        seckillOrder.setId(idWorker.nextId());
//        seckillOrder.setSeckillId(id);
//        seckillOrder.setMoney(goods.getCostPrice());
//        seckillOrder.setUserId(username);
//        seckillOrder.setCreateTime(new Date());
//        seckillOrder.setStatus("0");
//
//        //将秒杀商品存到redis中
//        redisTemplate.boundHashOps(SystemConstants.SEC_KILL_ORDER_KEY).put(username,seckillOrder);
//        //库存减少
//        goods.setStockCount(goods.getStockCount() - 1);
//        //判断当前商品是否还有库存
//        if (goods.getStockCount() <= 0){
//            //将数据同步到mysql中
//            seckillGoodsDao.updateById(goods);
//            //如果没有库存,则清空Redis缓存中该商品
//            redisTemplate.boundHashOps(SystemConstants.SEC_KILL_GOODS_PREFIX + time).delete(id);
//        }else {
//            //如果有库存，则直数据重置到Reids中
//            redisTemplate.boundHashOps(SystemConstants.SEC_KILL_GOODS_PREFIX + time).put(id,goods);
//        }
        return true;
    }
}
